package zhizhemodi.tech.short_term_bwork.service;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import zhizhemodi.tech.short_term_bwork.entity.DirFile;
import zhizhemodi.tech.short_term_bwork.mapper.DirFileMapper;

@Service
public class DirFileService {
    
    private String property = System.getProperty("user.dir");
    private static String BASE_PATH = "src//main//resources//localStorage";

    @Autowired
    DirFileMapper dirFileMapper;

    public DirFile getFile(
        String fileName,
        Integer fileId
    ) {
        QueryWrapper<DirFile> queryWrapper = new QueryWrapper<>();
        if(fileName != null) queryWrapper.eq("file_name", fileName);
        if(fileId > 0) queryWrapper.eq("file_id", fileId);
        return dirFileMapper.selectOne(queryWrapper);
    }

    /**
     * 获取匹配的文件
     * @param keyWord
     * @param dirUrl
     * @param type
     * @param dId
     * @return
     */
    public List<DirFile> getFileList(
        String keyWord,
        String dirUrl,
        Integer type,
        Integer dId
    ) {
        QueryWrapper<DirFile> queryWrapper = new QueryWrapper<>();
        if(keyWord != null) queryWrapper.like("file_name", keyWord);
        if(dirUrl != null) queryWrapper.eq("dir_url", dirUrl);
        if(type >= 0) queryWrapper.eq("type", type);
        queryWrapper.eq("department_id", dId);
        return dirFileMapper.selectList(queryWrapper);
    }

    /**
     * 新增文件夹
     * @param dirFile
     * @throws Exception
     */
    public void addFile(DirFile dirFile) throws Exception {
        if(certainFileSearch(dirFile.getFileName(), dirFile.getDirUrl()) != null) throw new Exception("文件夹已存在");
        List<DirFile> dList = dirFileMapper.selectList(null);
        Integer fileId = -1;
        if(dList.size() > 0) fileId = dList.get(dList.size() - 1).getFileId();
        dirFile.setFileId(fileId + 1);
        dirFileMapper.insert(dirFile);
    }

    /**
     * 删除文件
     * @param url
     */
    public void deleteFile(String url) throws Exception{
        String[] path = url.split("/");
        String fileName = path[path.length - 1];
        String dirUrl = "";
        for(int i = 1;i < path.length - 1; i++) {
            dirUrl = dirUrl + "/" + path[i];
        }
        DirFile curDirFile = certainFileSearch(fileName, dirUrl);
        if(curDirFile.getIsDirectory() == true) {
            List<DirFile> dList = getFileList("", curDirFile.getDirUrl() + "/" + curDirFile.getFileName(), -1, curDirFile.getDepartmentId());
            for(DirFile file : dList) {
                deleteFile(url + "/" + file.getFileName());
            }
        }
        File file = new File(property + "//" + BASE_PATH, url);
        try{
            dirFileMapper.deleteById(curDirFile);
            if(file.exists()) file.delete();
        } catch(Exception e) {
            throw new Exception("删除失败:" + e.getMessage());
        }
    }

    public DirFile certainFileSearch(String fileName, String dirUrl) {
        QueryWrapper<DirFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_name", fileName);
        queryWrapper.eq("dir_url", dirUrl);
        return dirFileMapper.selectOne(queryWrapper);
    }
}
